package com.chuji.crmcj.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chuji.crmcj.R;
import com.chuji.crmcj.bean.SalesPersonInfo;
import com.chuji.crmcj.fragment.MagClientFollowFragment;
import com.chuji.crmcj.fragment.MagPossibleClientFragment;
import com.chuji.crmcj.fragment.MagTenuerClientFragment;
import com.chuji.crmcj.utils.NumberUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;
import com.jauker.widget.BadgeView;
import com.nineoldandroids.view.ViewHelper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class SalesPersonActivity extends BaseActivity implements View.OnClickListener{
    //返回
    private ImageView mIv_Back;
    /*跟进记录网络存储*/
    private List<SalesPersonInfo.ApiParamObjEntity> salesPersonData;
    /*跟进记录数据*/
    private SalesPersonInfo salesPersonInfo;
    //销售员头像
    private ImageView mIv_salesman_head;
    //跟进客户
    private Button mBtn_client_follow;
    //潜在客户
    private Button mBtn_possible_client;
    //销售员姓名
    private TextView tv_salesman_name;
    //销售业绩
    private TextView tv_salesman_score;
    //保有客户
    private Button mBtn_retain_client;
    //下划线
    private View mSales_indicate_line;
    //填充数据
    private ViewPager mVp_sales;
    /*碎片集合*/
    private List<Fragment> fragments;
    //销售员ID
    String SalesID;
    //用户ID
    String UserID;
    //意图
    Intent intent;
    private android.app.FragmentManager manager;
    private android.app.FragmentTransaction transaction;
    BadgeView badgeView;
    @Override
    protected void onStart() {
        super.onStart();
        //数据
        InitTextView();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sales_person);
        //初始化组件
        mIv_Back = (ImageView) findViewById(R.id.iv_Back);
        mIv_salesman_head = (ImageView) findViewById(R.id.iv_salesman_head);
        mBtn_client_follow = (Button) findViewById(R.id.btn_client_follow);
        mBtn_possible_client = (Button) findViewById(R.id.btn_possible_client);
        mBtn_retain_client = (Button) findViewById(R.id.btn_retain_client);
        mSales_indicate_line = (View) findViewById(R.id.sales_indicate_line);
        tv_salesman_score= (TextView) findViewById(R.id.tv_salesman_score);
        tv_salesman_name= (TextView) findViewById(R.id.tv_salesman_name);
        mVp_sales = (ViewPager) findViewById(R.id.vp_sales);
        //接收上个页面的信息
        intent=getIntent();
        SalesID= intent.getStringExtra("SalesID");
        UserID= intent.getStringExtra("UserID");
        badgeView = new BadgeView(SalesPersonActivity.this);

        //获取头像
        if (intent.getStringExtra("avatar")==null){
            mIv_salesman_head.setImageResource(R.drawable.morentouxiang);
        }else {
            Glide.with(UIUtils.getContext())
                    .load(UrlUtils.ImageIP + intent.getStringExtra("avatar"))
                    .into(mIv_salesman_head);
        }
        String position=intent.getStringExtra("position");
        if(position.equals("0")){
            badgeView.setTargetView(mIv_salesman_head);
            badgeView.setBadgeGravity(Gravity.RIGHT | Gravity.BOTTOM);
            badgeView.setBackgroundResource(R.drawable.paiming1);
//                badgeView.setBackground(10, Color.parseColor("#ff0000"));
            badgeView.setText("");
        }else if(position.equals("1")){
            badgeView.setTargetView(mIv_salesman_head);
            badgeView.setBadgeGravity(Gravity.RIGHT | Gravity.BOTTOM);
            badgeView.setBackgroundResource(R.drawable.paiming2);
//                badgeView.setBackground(10, Color.parseColor("#ff0000"));
            badgeView.setText("");
        }else if(position.equals("2")){
            badgeView.setTargetView(mIv_salesman_head);
            badgeView.setBadgeGravity(Gravity.RIGHT | Gravity.BOTTOM);
            badgeView.setBackgroundResource(R.drawable.paiming3);
//                badgeView.setBackground(10, Color.parseColor("#ff0000"));
            badgeView.setText("");
        }else {

        }
        //获取姓名
        tv_salesman_name.setText(intent.getStringExtra("name"));
        //获取业绩
        BigDecimal bd = new BigDecimal(intent.getStringExtra("Performance"));
        tv_salesman_score.setText(NumberUtils.parseMoney(",###,###.##", bd));
        manager = getFragmentManager();
        transaction = manager.beginTransaction();
        //点击事件监听
        initListener();
        //设置监听
        mIv_Back.setOnClickListener(this);
        mBtn_client_follow.setOnClickListener(this);
        mBtn_possible_client.setOnClickListener(this);
        mBtn_retain_client.setOnClickListener(this);
        //设置滑动监听
        mVp_sales.setOnPageChangeListener(new OnMainPageChangeListener());

        Bundle bundle1 = new Bundle();
        bundle1.putString("SalesID",SalesID);
        new MagClientFollowFragment().setArguments(bundle1);

        Bundle bundle2 = new Bundle();
        bundle2.putString("SalesID",SalesID);
        new MagPossibleClientFragment().setArguments(bundle2);

        Bundle bundle3 = new Bundle();
        bundle2.putString("SalesID", SalesID);
        new MagTenuerClientFragment().setArguments(bundle3);
    }
    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_Back:
                finish();
                break;
            case R.id.btn_client_follow:
                mVp_sales.setCurrentItem(0);
                break;
            case R.id.btn_possible_client:
                mVp_sales.setCurrentItem(1);
                break;
            case R.id.btn_retain_client:
                mVp_sales.setCurrentItem(2);
                break;
        }
    }
    /**
     * 数据
     */
    protected void InitTextView() {
           /*详情界面的两个功能  填充效果*/
        fragments = new ArrayList<Fragment>();
        fragments.add(new MagClientFollowFragment());
        fragments.add(new MagPossibleClientFragment());
        fragments.add(new MagTenuerClientFragment());
        MyPageAdapter myPageAdapter = new MyPageAdapter(getSupportFragmentManager(),fragments);
        mVp_sales.setAdapter(myPageAdapter);
        // 高亮第一个标签
        updateTabs(0);
        // 初始化指示器宽度
        int screenW = getWindowManager().getDefaultDisplay().getWidth();
        mSales_indicate_line.getLayoutParams().width = screenW / 3;
        mSales_indicate_line.requestLayout(); // 重新计算大小，并刷新控件
    }

    /**
     * 高亮position选中页面对应的标签，并将其他的变暗
     */
    private void updateTabs(int position) {
        updateTab(position, 0, mBtn_client_follow);
        updateTab(position, 1, mBtn_possible_client);
        updateTab(position, 2, mBtn_retain_client);
    }

    /**
     * 判断当前要处理的 tabPosition 是否是选中的 position，并修改tab的高亮状态
     */
    private void updateTab(int position, int tabPosition, TextView tab) {
        int blue = getResources().getColor(R.color.blue);
        int halfWhite = getResources().getColor(R.color.font3);
        if (position == tabPosition) {
            // tab对应的页面被选中
            tab.setTextColor(blue);
        } else {
            // tab对应的页面没有被选中
            tab.setTextColor(halfWhite);
        }
    }

    public class MyPageAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragments;

        public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    private class OnMainPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        /** 当touch事件发生时回调此方法 */
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // 偏移位置 = 手指划过屏幕的百分比 * 指示器宽度
            int offsetX = (int) (positionOffset * mSales_indicate_line.getWidth());
            // 起始位置 = position * 指示器宽度
            int startX = position * mSales_indicate_line.getWidth();
            // 指示器移动的位置 = 起始位置 + 偏移位置
            int translationX = startX + offsetX;
            ViewHelper.setTranslationX(mSales_indicate_line, translationX);
        }

        @Override
        /** 当见面选中状态发生变更时会回调此方法 */
        public void onPageSelected(int position) {
            // 高亮选中页面对应的标签，并将其他的变暗
            updateTabs(position);
        }

        @Override
        /** 当页面的滑动状态发生变更会回调此方法 */
        public void onPageScrollStateChanged(int state) {

        }
    }
}
